**Opening First Dataset 
use "/Users/lewiskrashnsky/Documents/Princeton/Chris Referendum Project/Data/BC Merged Data/Second Period/2009 Election.dta"

**Merging 
merge m:m EDVA_CODE_F ED_ABBREVIATION using "/Users/lewiskrashnsky/Documents/Princeton/Chris Referendum Project/Data/BC Merged Data/Second Period/2013 Election.dta" 

rename _merge _merge1

merge m:m EDVA_CODE_F ED_ABBREVIATION using "/Users/lewiskrashnsky/Documents/Princeton/Chris Referendum Project/Data/BC Merged Data/Second Period/2009 Referendum.dta" 

rename _merge _merge2 


*Check for Duplicates
duplicates report EDVA_CODE_F
duplicates examples EDVA_CODE_F

**Dropping absentee, mail-in-ballots, special ballots, voting in DEO office (reason: cannot satisfy assumption that same individuals are voting with this method in multiple years)**
drop if strmatch(EDVA_CODE_F, "*Absentee*")
drop if strmatch(EDVA_CODE_F, "*Voting in DEO office*")
drop if strmatch(EDVA_CODE_F, "*Voting by mail*")
drop if strmatch(EDVA_CODE_F, "*Special*")


***Cleaning data: Checking for odd vote totals between referendum and concurrent election in 2009***
gen weird_total = Total_Vote_2009-Total_Ref_Votes_2009
summarize weird_total 

gen weird_total_percent = (Total_Vote_2009-Total_Ref_Votes_2009)/Total_Vote_2009
summarize weird_total_percent

gen check_story = weird_total_percent if _merge1==3 & _merge2==3 & weird_total_percent>=.10 | weird_total_percent<=-.10

**Dropping weird totals that cannot be accounted for (6 observations total out of 11,840)
drop in 9761
drop in 5284
drop in 3865
drop in 2127
drop in 1161
drop in 351

***Cleaning data: checking if vote totals are accurate***
**Both 2009 and 2013 Election Vote Totals made manually, so not necessary to check vote totals of downloaded set
*Checking if 2009 referendum Vote totals line up 
egen check_ref_vote_total = rowtotal(FPTP_Vote_Ref_2009 BCSTV_Vote_Ref_2009)

gen Ref_2009_vote_diff = check_ref_vote_total-Total_Ref_Votes_2009 

gen problematic_total_2009_ref = Ref_2009_vote_diff if Ref_2009_vote_diff != 0

*summarize: one observation with bad total 
summarize problematic_total_2009_ref

*Manually fixing bad observation total (wrong addition)
replace Total_Ref_Votes_2009 = 30 in 11282
(1 real change made)

**Dropping vote total checking variables 
drop problematic_total_2009_ref
drop Ref_2009_vote_diff
drop check_ref_vote_total 


*Ensuring Percentage variables are clean (zero real changes made)
replace Percent_Liberal_Vote_2009 = . if Percent_Liberal_Vote_2009 >1
replace Percent_Liberal_Vote_2009 = . if Percent_Liberal_Vote_2009<0

replace Percent_Liberal_Vote_2013 = . if Percent_Liberal_Vote_2013>1
replace Percent_Liberal_Vote_2013 = . if Percent_Liberal_Vote_2013<0

replace FPTP_Vote_Ref_Percent_2009  = . if FPTP_Vote_Ref_Percent_2009 >1
replace FPTP_Vote_Ref_Percent_2009  = . if FPTP_Vote_Ref_Percent_2009 <0


**Checking for Outlers 
scatter Liberal_Party_Vote_2009 Liberal_Party_Vote_2013

scatter Liberal_Party_Vote_2009 FPTP_Vote_Ref_2009
scatter Liberal_Party_Vote_2013 FPTP_Vote_Ref_2009

scatter Percent_Liberal_Vote_2009 Percent_Liberal_Vote_2013
scatter Percent_Liberal_Vote_2009 FPTP_Vote_Ref_Percent_2009 
scatter Percent_Liberal_Vote_2013 FPTP_Vote_Ref_Percent_2009


**Creating new variables for analysis
*NDP Vote Percentage
gen Percent_NDP_Vote_2009 = NDP_Vote_2009/Total_Vote_2009
gen Percent_NDP_Vote_2013 = NDP_Vote_2013/Total_Vote_2013


*Green Party Vote Percentage 
gen Percent_Green_Vote_2009 = Green_Party_Vote_2009/Total_Vote_2009
gen Percent_Green_Vote_2013 = Green_Party_Vote_2013/Total_Vote_2013


***Analysis***

*Scatter plots: Liberal Voting 
graph twoway (lfit FPTP_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2009) (scatter FPTP_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2009)

graph export "/Users/lewiskrashnsky/Desktop/FPTP_Vote_and_2009_Liberal_Vote.pdf", as(pdf) name("Graph")
file /Users/lewiskrashnsky/Desktop/FPTP_Vote_and_2009_Liberal_Vote.pdf saved as PDF format
	
graph twoway (lfit FPTP_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2013) (scatter FPTP_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2013)

graph export "/Users/lewiskrashnsky/Desktop/FPTP_2009_and_2013_Liberal_Vote.pdf", as(pdf) name("Graph")
file /Users/lewiskrashnsky/Desktop/FPTP_2009_and_2013_Liberal_Vote.pdf saved as PDF format


*Scatter plots: NDP Voting 
graph twoway (lfit FPTP_Vote_Ref_Percent_2009 Percent_NDP_Vote_2009) (scatter FPTP_Vote_Ref_Percent_2009 Percent_NDP_Vote_2009)

graph export "/Users/lewiskrashnsky/Desktop/FPTP_Percent_and_NDP_2009.pdf", as(pdf) name("Graph")
file /Users/lewiskrashnsky/Desktop/FPTP_Percent_and_NDP_2009.pdf saved as PDF format


graph twoway (lfit FPTP_Vote_Ref_Percent_2009 Percent_NDP_Vote_2013) (scatter FPTP_Vote_Ref_Percent_2009 Percent_NDP_Vote_2013)

graph export "/Users/lewiskrashnsky/Desktop/FPTP_2009_and_NDP_Vote_2013.pdf", as(pdf) name("Graph")
file /Users/lewiskrashnsky/Desktop/FPTP_2009_and_NDP_Vote_2013.pdf saved as PDF format


*Scatter plots: Green Party Voting 
graph twoway (lfit FPTP_Vote_Ref_Percent_2009 Percent_Green_Vote_2009) (scatter FPTP_Vote_Ref_Percent_2009 Percent_Green_Vote_2009)

graph export "/Users/lewiskrashnsky/Desktop/FPTP_2009_and_Green_Vote_2009.pdf", as(pdf) name("Graph")
file /Users/lewiskrashnsky/Desktop/FPTP_2009_and_Green_Vote_2009.pdf saved as PDF format


graph twoway (lfit FPTP_Vote_Ref_Percent_2009 Percent_Green_Vote_2013) (scatter FPTP_Vote_Ref_Percent_2009 Percent_Green_Vote_2013)

graph export "/Users/lewiskrashnsky/Desktop/FPTP_2009_and_Green_Vote_2013.pdf", as(pdf) name("Graph")
file /Users/lewiskrashnsky/Desktop/FPTP_2009_and_Green_Vote_2013.pdf saved as PDF format
	
	
**OLS Models
*Liberal Vote Models 
regress FPTP_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2009, robust 
outreg2 using fptp_liberal_2009.doc, replace ctitle (FPTP Percentage 2009 (OLS))

regress FPTP_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2009 Total_Vote_2009, robust

regress FPTP_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2013, robust
outreg2 using fptp_liberal_2013.doc, replace ctitle (FPTP_Percentage_2009 (OLS))

regress FPTP_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2013 Total_Vote_2013, robust


*NDP Vote Models 
regress FPTP_Vote_Ref_Percent_2009 Percent_NDP_Vote_2009, robust 
outreg2 using fptp_ndp_2009.doc, replace ctitle (FPTP Percentage 2009 (OLS))

regress FPTP_Vote_Ref_Percent_2009 Percent_NDP_Vote_2013, robust
outreg2 using fptp_ndp_2013.doc, replace ctitle (FPTP_Percentage_2009 (OLS)) 




**Average support among Liberal  vote subsets**
mean FPTP_Vote_Ref_Percent_2009 if Percent_Liberal_Vote_2009>=.2 & Percent_Liberal_Vote_2009<=.3 

mean FPTP_Vote_Ref_Percent_2009 if Percent_Liberal_Vote_2009>=.4 & Percent_Liberal_Vote_2009<=.5

mean FPTP_Vote_Ref_Percent_2009 if Percent_Liberal_Vote_2009>=.7 & Percent_Liberal_Vote_2009<=.8


mean BCSTV_Vote_Ref_Percent_2009 if Percent_Liberal_Vote_2009>=.2 & Percent_Liberal_Vote_2009<=.3 

mean BCSTV_Vote_Ref_Percent_2009 if Percent_Liberal_Vote_2009>=.4 & Percent_Liberal_Vote_2009<=.5

mean BCSTV_Vote_Ref_Percent_2009 if Percent_Liberal_Vote_2009>=.7 & Percent_Liberal_Vote_2009<=.8




**Average support among NDP vote subsets** 

mean FPTP_Vote_Ref_Percent_2009 if Percent_NDP_Vote_2009>=.2 & Percent_NDP_Vote_2009<=.3

mean FPTP_Vote_Ref_Percent_2009 if Percent_NDP_Vote_2009>=.4 & Percent_NDP_Vote_2009<=.5

mean FPTP_Vote_Ref_Percent_2009 if Percent_NDP_Vote_2009>=.7 & Percent_NDP_Vote_2009<=.8

 

 ***New Figures and Regression Models for Analysis with DV switched to BCSTV Support 
 
*creating new DV 
gen BCSTV_Vote_Ref_Percent_2009 = BCSTV_Vote_Ref_2009/Total_Ref_Votes_2009

**New figures 
graph twoway (lfit BCSTV_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2009) (scatter BCSTV_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2009)

graph twoway (lfit BCSTV_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2013) (scatter BCSTV_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2013)

graph twoway (lfit BCSTV_Vote_Ref_Percent_2009 Percent_NDP_Vote_2009) (scatter BCSTV_Vote_Ref_Percent_2009 Percent_NDP_Vote_2009)

graph twoway (lfit BCSTV_Vote_Ref_Percent_2009 Percent_NDP_Vote_2013) (scatter BCSTV_Vote_Ref_Percent_2009 Percent_NDP_Vote_2013)

graph twoway (lfit BCSTV_Vote_Ref_Percent_2009 Percent_Green_Vote_2009) (scatter BCSTV_Vote_Ref_Percent_2009 Percent_Green_Vote_2009)

graph twoway (lfit BCSTV_Vote_Ref_Percent_2009 Percent_Green_Vote_2013) (scatter BCSTV_Vote_Ref_Percent_2009 Percent_Green_Vote_2013)

*New Regression Models 
regress BCSTV_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2009, robust 
outreg2 using bcstv_liberal_2009.doc, replace ctitle (BCSTV Percentage 2009 (OLS))

regress BCSTV_Vote_Ref_Percent_2009 Percent_NDP_Vote_2009, robust 
outreg2 using bcstv_ndp_2009.doc, replace ctitle (BCSTV Percentage 2009 (OLS))


**Figures with Lowess fit** 
lowess BCSTV_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2009

lowess BCSTV_Vote_Ref_Percent_2009 Percent_NDP_Vote_2009

**Trying normal transformation of data** 
gen normal_BCSTV_percent_vote = invnormal(BCSTV_Vote_Ref_Percent_2009)

gen normal_Liberal_2009_percent_vote = invnormal(Percent_Liberal_Vote_2009)

gen normal_NDP_2009_vote = invnormal(Percent_NDP_Vote_2009)

*Figures of normal transformations of data 
lowess normal_BCSTV_percent_vote normal_Liberal_2009_percent_vote

lowess normal_BCSTV_percent_vote normal_NDP_2009_vote



***Multivariate Regression models
regress BCSTV_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2009 Percent_NDP_Vote_2009 Percent_Green_Vote_2009, robust 
outreg2 using Multivariate_2009_Models.doc, replace ctitle (Percentage Support for Electoral Reform, 2009 Referendum (OLS))

regress BCSTV_Vote_Ref_Percent_2009 Percent_Liberal_Vote_2009 Percent_Green_Vote_2009, robust 
outreg2 using Multivariate_2009_Models_2.doc, replace ctitle (Percentage Support for Electoral Reform, 2009 Referendum (OLS))

regress BCSTV_Vote_Ref_Percent_2009 Percent_NDP_Vote_2009 Percent_Green_Vote_2009, robust 
outreg2 using Multivariate_2009_Models_3.doc, replace ctitle (Percentage Support for Electoral Reform, 2009 Referendum (OLS))

**Final model 
gen cons_2009 = Conservative_Vote_2009/Total_Vote_2009
replace cons_2009 = 0 if cons_2009==.

gen Lib_Con_2009_frac = (cons_2009 + Percent_Liberal_Vote_2009)

*Unweighted
regress BCSTV_Vote_Ref_Percent_2009 Lib_Con_2009_frac, robust 
outreg2 using Supplementary_2009_Regression_Unweighted.doc, replace ctitle (Percentage Support for Electoral Reform, 2009 Referendum (OLS))

*Weighted
regress BCSTV_Vote_Ref_Percent_2009 Lib_Con_2009_frac [pweight= Total_Vote_2009], robust 
outreg2 using Supplementary_2009_Regression_Weighted.doc, replace ctitle (Percentage Support for Electoral Reform, 2009 Referendum (OLS))
